/* ====================================================================
 Small program for iterating through a 4x4 led matrix.
 Easaxen.com 2013
 ====================================================================*/

int clockPin = 12;
int dataPin = 11;
int latchPin = 8;

void setup() {  
  Serial.begin(9600);
  pinMode(clockPin,OUTPUT);
  pinMode(dataPin,OUTPUT);
  pinMode(latchPin,OUTPUT);
}

void sendByte(byte b8){
    byte tmp = 0b10000000; // Start on number 128  
    //Serial.println(tmp,BIN);
    for(int j=0;j<8;j++){
      
      if ((b8 & tmp) == tmp){
        digitalWrite(dataPin,HIGH);
        Serial.print("1"); //Serial monitor information
      }
      else{
        digitalWrite(dataPin,LOW);
        Serial.print("0"); //Serial monitor information
      }  
       tmp = tmp >> 1;
       digitalWrite(clockPin,HIGH);
        // delay(1); //Can be reduced further
       digitalWrite(clockPin,LOW);
       
    }
    Serial.print("\n");
}

void loop() {
  int i,j;
 
  //Iterate through the 8 controller pins
  for(i=0;i<4;i++)
  {
    for(j=0;j<4;j++)
    {
      digitalWrite(latchPin,LOW); //Keep low until new byte is stored
        byte xGND = 0b00000001 << i;
        byte yVCC = 0b00010000 << j;
        byte pins = yVCC | xGND; 
        sendByte(pins);
      digitalWrite(latchPin,HIGH); //Moves byte from shift register to storage register
      delay(100);
    }
  }
  

}
